/*
+--------------------------------------------------------------------------
|   modalWindow Plugin
|   =============================================
|   by Oliver Trebes
|   (c) 2006 - 2007 amplified webservices
|   =============================================
|   http://www.angelmedia.de
+--------------------------------------------------------------------------
|   > $Date$
|   > $Revision$
|   > $Author$
+--------------------------------------------------------------------------
|   > Filename:     modalWindow.js
|   > Date started: Tue Dec 18 11:49:23 CET 2007
+--------------------------------------------------------------------------
|	Title: Modal Window Plugin
|
|	Please see <copyright.inc.php> for a detailed description, copyright
|	and license information.
+--------------------------------------------------------------------------
|	javascript based on lightbox 1.0:
+--------------------------------------------------------------------------
|	Lokesh Dhakar - http://www.huddletogether.com
|
|	For more information on this script, visit:
|	http://huddletogether.com/projects/lightbox/
|
|	Licensed under the Creative Commons Attribution 2.5 License - 
|	http://creativecommons.org/licenses/by/2.5/
+--------------------------------------------------------------------------
*/

	try {
		if (undefined == xajax.ext)
			xajax.ext = {};
	} catch (e) {
		1("Could not create xajax.ext namespace");
	}

	try {
		if (undefined == xajax.ext.modalWindow)
			xajax.ext.modalWindow = {};
	} catch (e) {
		alert("Could not create xajax.ext.modalWindow namespace");
	}

	xjxmW = xajax.ext.modalWindow;
	
	var lb_widgets = 0;

	xjxmW.getWindowWidth = function( objDoc, objWin )
	{
		return (objDoc.layers||(objDoc.getElementById&&!objDoc.all)) ? objWin.innerWidth : (objDoc.all ? objDoc.body.clientWidth : 0);
	} 
	xjxmW.getWindowHeight = function( objDoc, objWin )
	{
		return objWin.innerHeight ? objWin.innerHeight :(objDoc.getBoxObjectFor ? Math.min(objDoc.documentElement.clientHeight, objDoc.body.clientHeight) : ((objDoc.documentElement.clientHeight != 0) ? objDoc.documentElement.clientHeight : (objDoc.body ? objDoc.body.clientHeight : 0)));
	} 
	xjxmW.getScrollWidth = function( objDoc, objWin )
	{
		return objDoc.all ? Math.max(Math.max(objDoc.documentElement.offsetWidth, objDoc.documentElement.scrollWidth), objDoc.body.scrollWidth) : (objDoc.body ? objDoc.body.scrollWidth : ((objDoc.documentElement.scrollWidth != 0) ? objDoc.documentElement.scrollWidth : 0));
	} 
	xjxmW.getScrollHeight = function( objDoc, objWin )
	{
		return objDoc.all ? Math.max(Math.max(objDoc.documentElement.offsetHeight, objDoc.documentElement.scrollHeight), Math.max(objDoc.body.offsetHeight, objDoc.body.scrollHeight)) : (objDoc.body ? objDoc.body.scrollHeight : ((objDoc.documentElement.scrollHeight != 0) ? objDoc.documentElement.scrollHeight : 0));
	} 
	xjxmW.getScrollLeft = function( objDoc, objWin )
	{
		return objDoc.all ? (!objDoc.documentElement.scrollLeft ? objDoc.body.scrollLeft : objDoc.documentElement.scrollLeft) : ((objWin.pageXOffset != 0) ? objWin.pageXOffset : 0);
	} 
	xjxmW.getScrollTop = function( objDoc, objWin )
	{
		return objDoc.all ? (!objDoc.documentElement.scrollTop ? objDoc.body.scrollTop : objDoc.documentElement.scrollTop) : ((objWin.pageYOffset != 0) ? objWin.pageYOffset : 0);
	} 
	 
	/**
	 * Function getSize to recieve the size of the current window
	 */
	xjxmW.getSize = function( objDoc, objWin )
	{
		WindowWidth   = xjxmW.getWindowWidth ( objDoc, objWin );
		WindowHeight  = xjxmW.getWindowHeight( objDoc, objWin );
		ScrollWidth   = xjxmW.getScrollWidth ( objDoc, objWin );
		ScrollHeight  = xjxmW.getScrollHeight( objDoc, objWin );
		ScrollLeft    = xjxmW.getScrollLeft  ( objDoc, objWin );
		ScrollTop     = xjxmW.getScrollTop   ( objDoc, objWin );
		
		PageWidth  = ScrollWidth > WindowWidth ? ScrollWidth : WindowWidth;
		PageHeight = ScrollHeight > WindowHeight ? ScrollHeight : WindowHeight;
		
		return new Array(PageWidth,PageHeight,WindowWidth,WindowHeight, ScrollWidth, ScrollHeight, ScrollLeft, ScrollTop);
	} 
		
	xjxmW.getElementSize = function ( e )
	{
		if ( e.display != 'none' && e.display != null )
		{
			return {width: e.offsetWidth, height: e.offsetHeight};
		}
		else
		{
		    var originalVisibility	= e.style.visibility;
		    var originalPosition 	= e.style.position;
		    var originalDisplay 	= e.style.display;
		    e.style.visibility 		= 'hidden';
		    e.style.position 		= 'absolute';
		    e.style.display 		= 'block';
		    var sizes 				= {width: e.clientWidth, height: e.clientHeight};
		    var originalHeight 		= e.clientHeight;
		    e.style.display 		= originalDisplay;
		    e.style.position 		= originalPosition;
		    e.style.visibility 		= originalVisibility;
		    return sizes;
	 	}  
	}	
		
	/**
	 * Function mw:aw add a new modal window
	 */
	xjxmW.addWindow = function( args )
	{

		var objDoc			= document;
		var objWin			= window;
		var pageOverFlow	= 100; 
		
		if( parent.frames.length && args.data[4].length > 0 )
		{
			objDoc = args.data[4] == 'parent' ? parent.document : parent.frames[args.data[4]].document;
			objWin = args.data[4] == 'parent' ? parent : parent.frames[args.data[4]];		
		}
		if ( !objDoc.lb_widgets )
		{
			objDoc.lb_widgets = 0;
		}
		
		xjxmW.hideSelects( 'hidden', objDoc );

		objDoc.lb_widgets++;

		var objBody			= objDoc.getElementsByTagName("body").item(0) ? objDoc.getElementsByTagName("body").item(0) : objDoc.getElementsByTagName("html").item(0);
		var zIndex			= objDoc.lb_widgets ? objDoc.lb_widgets * 1000 : 1000;
		var arrayPageSize	= xjxmW.getSize( objDoc, objWin );
		var objOverlay 		= objDoc.createElement("div");

		objOverlay.setAttribute('id','lb_layer' + objDoc.lb_widgets );
		objOverlay.style.display 	= 'none';
		objOverlay.style.position	= 'absolute';
		objOverlay.style.top		= '0';
		objOverlay.style.left		= '0';
		objOverlay.style.zIndex		= zIndex;
	 	objOverlay.style.width		= '100%';
	 	objOverlay.style.height		= (arrayPageSize[1] + 'px');
	 	objOverlay.style.minHeight	= '100%';

	 	if ( args.data[1] && args.data[1] != null )
	 	{
	 		objOverlay.style.backgroundColor = args.data[1];
	 	}
	 	
	 	if ( args.data[2] )
	 	{
	 		objOverlay.style.display = '';
	 	
			if (navigator.appVersion.indexOf("MSIE")!=-1)
			{
				objOverlay.style.filter = "alpha(opacity=" + args.data[2] + ")";
			}
			else
			{
	 			objOverlay.style.opacity = ( args.data[2] / 100 );
			}
	 	}	 	
	 	
	 	if ( args.data[3] )
	 	{
			objOverlay.className = args.data[3];
	 	}	 	
	 	
	 	if ( args.data[5] )
	 	{
			pageOverFlow = parseInt(args.data[5]);
	 	}	 	

	 	objBody.appendChild(objOverlay);
		
		var objLockbox = objDoc.createElement("div");
		objLockbox.setAttribute('id','lb_content' + objDoc.lb_widgets );
		objLockbox.style.visibility	= 'hidden';
		objLockbox.style.position	= 'absolute';
		objLockbox.style.top		= '0';
		objLockbox.style.left		= '0';
		objLockbox.style.zIndex		= zIndex + 1 ;	
		
		objBody.appendChild(objLockbox);
		
		objLockbox.innerHTML = args.data[0];
		
		var objContent = objLockbox.firstChild;
		
		var sizes = xjxmW.getElementSize( objContent ); 
	
		if ( sizes.height > arrayPageSize[1] )
		{
			arrayPageSize[3] = sizes.height + pageOverFlow;
			arrayPageSize[1] = sizes.height + pageOverFlow;
		}
	
		objOverlay.style.height		= (arrayPageSize[1] + 'px');

		cltop   = (arrayPageSize[7] + 	( (arrayPageSize[3] -  sizes.height ) / 2 ) );
		clleft	= (                     ( (arrayPageSize[0] -  sizes.width ) / 2 ) );

		objLockbox.style.top  = cltop  < 0 ? '0px' : cltop  + 'px';
		objLockbox.style.left = clleft < 0 ? '0px' : clleft + 'px';
		
		objLockbox.style.visibility = '';
	}
	
	/**
	 * Function mw:cw remove the highest modal window
	 */
	xjxmW.closeWindow = function( args )
	{
		var objDoc			= document;
		
		if( parent.frames.length )
		{
			if ( args == undefined )
			{
				args = 'parent';
			}
			objDoc = args == 'parent' ? parent.document : parent.frames[args].document;	
		}
		
		var activewidget = objDoc.lb_widgets;
		
		
		lId = 'lb_layer' + activewidget;
		cId = 'lb_content' + activewidget;
	
		objElement = objDoc.getElementById(cId);
		
		if (objElement && objElement.parentNode && objElement.parentNode.removeChild)
		{
			objElement.parentNode.removeChild(objElement);
		}	
		
		objElement = objDoc.getElementById(lId);
		
		if (objElement && objElement.parentNode && objElement.parentNode.removeChild)
		{
			objElement.parentNode.removeChild(objElement);
			objDoc.lb_widgets--;
			xjxmW.hideSelects('', objDoc); 
		}
	}
	/**
	 * Function closeWindow is an alias for mw:cw
	 */
	
	xajax.command.handler.register('mw:aw', function(args) {
		xajax.ext.modalWindow.addWindow(args);
		return true;
	});	
	xajax.command.handler.register('mw:cw', function(args) {
		xajax.ext.modalWindow.closeWindow(args);
		return true;
	});	
	
	/**
	 * Function hideSelects hide or show select-boxes expect the top layer
	 * this is an ie6-fix function
	 */
	xjxmW.hideSelects = function( visibility, objDoc )
	{
		var selects = document.getElementsByTagName('select');
		
		for(i = 0; i < selects.length; i++)
		{
			if ( !selects[i].rel )
			{
				selects[i].rel = 'ddl_' + objDoc.lb_widgets;
			}
			
			if ( selects[i].rel == 'ddl_' + objDoc.lb_widgets )
			{
				selects[i].style.visibility = visibility;
			}
			
			if ( visibility != 'hidden' )
			{
				selects[i].rel = null;
			}
		}
	}